package org.lalib.controller.action;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CCategoryForm;
import org.lalib.controller.service.CCategoryService;
import org.lalib.entity.object.ECategory;
import org.lalib.util.ApplicationContextUtil;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller(value = "cCategoryAction")
@Scope("request")
public class CCategoryAction implements IControllerAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	private CCategoryForm cCategoryForm;

	@Autowired
	private CCategoryService cCategoryService;

	public String init() {
		ECategory rootCategory = cCategoryService.getRootCategory();
		TreeNode rootNode = new DefaultTreeNode(new ECategory(), new DefaultTreeNode(new ECategory("Root"), null));
		recursionForTree(rootNode, rootCategory);
		cCategoryForm.setRootNode(rootNode);
		return "categoryDefination";
	}

	private void recursionForTree(TreeNode root, ECategory parentCategory) {
		for (ECategory category : parentCategory.getChildCategories()) {
			TreeNode treeNode = new DefaultTreeNode(category, root);
			recursionForTree(treeNode, category);
		}
	}

	public void changeSelection() {
		ECategory cat = (ECategory) cCategoryForm.getSelectedNode().getData();
		cCategoryForm.setSelectedCategory(cat);
	}

	public void myNewCategory() {
		cCategoryForm.setNewCategory(new ECategory());
		cCategoryForm.getNewCategory().setParentCategory(cCategoryForm.getSelectedCategory());
	}

	public void updateCategory() {
		cCategoryForm.setEditPanel(true);
		cCategoryForm.setSelectedCategoryId(cCategoryForm.getSelectedCategory().getParentCategory().getId());
		List<ECategory> categoryList = getAllCategoriesExceptRoot();
		List<SelectItem> categorySelectItemList = getCategorySelectItemList(categoryList);
		cCategoryForm.setSelectItemList(categorySelectItemList);
	}

	private List<SelectItem> getCategorySelectItemList(List<ECategory> categoryList) {
		List<SelectItem> selectItemList = new ArrayList<SelectItem>();
		for (ECategory category : categoryList)
			selectItemList.add(new SelectItem(category.getId(), category.getName()));
		return selectItemList;
	}

	private List<ECategory> getAllCategoriesExceptRoot() {
		return cCategoryService.getAllCategoriesExceptForRootCategory();
	}

	public void editCategory() {
		ECategory eCategory = cCategoryForm.getSelectedCategory();
		Long selectedCategoryId = cCategoryForm.getSelectedCategoryId();
		cCategoryService.update(eCategory, selectedCategoryId);
		cCategoryForm.setSelectedCategory(null);
		cCategoryForm.setSelectedNode(null);
		cCategoryForm.setEditPanel(false);
		init();
	}

	public void delete() {
		ECategory category = cCategoryForm.getSelectedCategory();
		delete(category);
		cCategoryForm.setSelectedCategory(null);
		cCategoryForm.setSelectedNode(null);
		init();
	}

	private void delete(ECategory category) {
		cCategoryService.delete(category);
	}

	public void addNewCategory() {
		ECategory category = cCategoryForm.getNewCategory();
		cCategoryService.save(category);
		init();
	}

	public String defineVariable() {
		CVariableAction cVariableAction = (CVariableAction) ApplicationContextUtil.getNewInstrance("cVariableAction");
		return cVariableAction.init(cCategoryForm.getSelectedCategory());
	}
}